package com.weina.controller.admin;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weina.entity.Employee;
import com.weina.service.EmployeeService;
import com.weina.utils.DataGridViewResult;
import com.weina.utils.SystemConstant;
import com.weina.vo.EmployeeVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.stylesheets.LinkStyle;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/employee")
public class EmployeeController {
    @Resource
    private EmployeeService employeeService;


    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session){
        Map<String, Object> map = new HashMap<>();
        //调用员工登录的方法
        Employee employee = employeeService.login(username, password);
        //判断对象是否为空，不为空标识登录成功
        if(employee!=null){
            session.setAttribute(SystemConstant.LOGINUSER,employee);
            map.put(SystemConstant.SUCCESS,true);
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"账号或密码错误");
        }
        return JSON.toJSONString(map);
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session, HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> map = new HashMap<String,Object>();
        session.invalidate();
        request.getSession().invalidate();
        map.put(SystemConstant.SUCCESS,true);
        map.put(SystemConstant.MESSAGE,"退出登录");
        return JSON.toJSONString(map);
    }
    @RequestMapping("/list")
    public DataGridViewResult employeList(EmployeeVo employeeVo){
        PageHelper.startPage(employeeVo.getPage(),employeeVo.getLimit());
        List<Employee> list=employeeService.getAllEmployee(employeeVo);
        PageInfo<Employee> page = new PageInfo<>(list);
        return new DataGridViewResult(page.getTotal(),page.getList());
    }
    //添加员工
    @RequestMapping("/addEmployee")
    public String addEmployee(Employee employee,HttpSession session){
        Map<String,Object> map = new HashMap<String,Object>();
        Employee loginUser=(Employee)session.getAttribute(SystemConstant.LOGINUSER);
        //设置创始人
        employee.setCreatedBy(loginUser.getId());
        if(employeeService.addEmployee(employee)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"添加成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"添加失败");
        }
        return JSON.toJSONString(map);
    }
    //修改员工
    @RequestMapping("/updateEmployee")
    public String updateEmployee(Employee employee,HttpSession session){
        Map<String,Object> map = new HashMap<String,Object>();
        Employee loginUser = (Employee) session.getAttribute(SystemConstant.LOGINUSER);
        employee.setModifyBy(loginUser.getId());
        if(employeeService.updateEmploye(employee)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"修改成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"修改失败");
        }
        return JSON.toJSONString(map);
    }
    //删除员工
    @RequestMapping("/deleteById/{id}")
    public String deleteEmployee(@PathVariable("id") Integer id){
        Map<String,Object> map = new HashMap<String,Object>();
        if(employeeService.deleteEmployeeById(id)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"员工删除成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"员工删除失败");
        }
        return JSON.toJSONString(map);
    }
    //重置密码
    @RequestMapping("/resetPwd")
    public String resetPwd(int id){
        Map<String,Object> map = new HashMap<String,Object>();
        if(employeeService.resetPwd(id)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"密码重置成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"密码重置失败");
        }
        return JSON.toJSONString(map);
    }
    //保存用户选择的角色列表
    @RequestMapping(value = "/saveEmployeeRole",method = RequestMethod.POST)
    public String saveEmployeeRole(@Param("roleStr") String roleStr,@Param("eid") Integer eid){
        Map<String,Object> map = new HashMap<String,Object>();
        if(employeeService.saveEmployeeRole(roleStr,eid)){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"角色分配成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"角色分配失败");
        }
        return JSON.toJSONString(map);
    }
}
